package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.OrderType;
import com.pojo.Orderdetail;
import com.pojo.Orders;
import com.service.OrderDetailService;
import com.service.OrderService;
import com.service.OrderTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.plugin.javascript.navig4.Window;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderDetailController {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderTypeService orderTypeService;
    @RequestMapping("/orderDetailList")
    public String showList(String uname,String cName,Model model,Integer page){
        if (page==null){
            page=1;
        }
        PageHelper.startPage(page,8);
        List<Orderdetail> list = orderDetailService.showList(uname,cName);
        PageInfo<Orderdetail> pageInfo=new PageInfo<Orderdetail>(list,5);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("uname",uname);
        model.addAttribute("cName",cName);
        return "refund";
    }
    @RequestMapping("/delOrderDetail")
    @ResponseBody
    public String delOrderDetail(int oid){
//        boolean b = orderDetailService.delOrderDetailByOid(oid);
//        boolean b1 = orderService.delOrder(oid);
        boolean b = orderService.delOrderDetailByOid(oid);
        if (b){
            return "ordersDetail";
        }
        return "ordersDetail";
    }
    @RequestMapping("/findByOid")
    @ResponseBody
    public Orderdetail findByOid(HttpSession session, int oid){
        Orderdetail orderdetail = orderDetailService.findByOid(oid);
//        session.setAttribute("Orderdetail",orderdetail);
        return orderdetail;
    }

    @RequestMapping("/updateOrder")
    @ResponseBody
    public boolean updateOrder(@RequestBody Orders orders){
        return orderService.updateOrder(orders);
    }

    @RequestMapping("/findByuNameAndcName")
    public String findByuNameAndcName(String uname,String cName,String orderStatus,Model model,Integer page){
        if (page==null){
            page=1;
        }
        PageHelper.startPage(page,8);
        List<Orderdetail> orderdetailList = orderDetailService.findByuNameAndcName(uname, cName, orderStatus);
        if (orderStatus=="0"){
            PageHelper.startPage(page,8);
            orderdetailList = orderDetailService.findByuNameAndcName(uname, cName, null);
        }
        else if (orderdetailList.size()==0){
            PageHelper.startPage(page,8);
            orderdetailList = orderDetailService.findByuNameAndcName(null, null, null);
//            model.addAttribute("message","没有找到相关信息");
        }
        PageInfo<Orderdetail> pageInfo=new PageInfo<Orderdetail>(orderdetailList,5);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("uname",uname);
        model.addAttribute("cName",cName);
        model.addAttribute("orderStatus",orderStatus);
        return "ordersDetail";
    }
    @RequestMapping("/updateOrderStatus")
    @ResponseBody
    public boolean updateOrderStatus(int oid){
        System.out.println(oid);
        return orderService.updateOrderStatus(oid);

    }

    @RequestMapping("/showOrderType")
    @ResponseBody
    public List<OrderType> showOrderType(HttpSession session){
        System.out.println(orderTypeService.showList());
        return orderTypeService.showList();
    }

}
